package com.thebox.config;

import java.util.*;
import java.io.*;

import org.apache.commons.digester.*;
import org.apache.commons.digester.xmlrules.*;
import org.apache.log4j.Logger;

import linuxbox.common.*;
import linuxbox.exception.*;

public class MainConfig {
    protected static Logger logger = Logger.getLogger
        (MainConfig.class.getName());

    private Map propMap;
    private List moduleNameList;
    private List moduleList;
    private Map moduleMap;
    private String inputPath = null;
    private String rulesPath = null;
    private Map actionMap = null;
    private ActionServlet servlet = null;
    private Map homepageModuleMap = null;
    private Map adminModuleMap = null;

    public MainConfig() {
        propMap = new HashMap();
        moduleNameList = new ArrayList();
        moduleList = new ArrayList();
        moduleMap = new HashMap();
        actionMap = new HashMap();
    }

    public void setProp(String name, String value) {
        //debug("name=" + name + " value=" + value);
        propMap.put(name.toUpperCase(), value);
    }

    public void setModule(String name, String isLoad) {
        if ("YES".equalsIgnoreCase(isLoad)) {
            //debug("add " + name);
            moduleNameList.add(name);
        }
    }

    public void setPath(String ip, String rp) {
        inputPath = ip;
        rulesPath = rp;
    }

    public Map getPropMap() {
        return propMap;
    }

    public void loadModule() throws Exception {
        for (Iterator it = moduleNameList.iterator(); it.hasNext(); ) {
            String name = (String)it.next();
            //debug("load " + name);
            File input = new File(inputPath + "/" + name);
            File rules = new File(rulesPath + "/ModuleConfigRule.xml");

            Digester digester = DigesterLoader.createDigester(rules.toURI().toURL());
            ModuleConfig mc = (ModuleConfig)digester.parse(input);
            mc.init();
            moduleList.add(mc);
        }

        for (Iterator it = moduleList.iterator(); it.hasNext(); ) {
            ModuleConfig mc = (ModuleConfig)it.next();
            moduleMap.put(mc.getName(), mc);
        }
    }

    public void setServlet(ActionServlet serv) {
        servlet = serv;
    }

    public linuxbox.common.Action getAction(String name) throws LinuxboxException {
        logger.debug("look for action=" + name);

        linuxbox.common.Action ret = (linuxbox.common.Action)actionMap.get(name);
        if (ret == null) {
            for (Iterator it = moduleList.iterator(); it.hasNext(); ) {
                ModuleConfig bm = (ModuleConfig)it.next();
                ret = bm.getAction(name);
                if (ret != null) break;
            }
            
            if (ret != null) {
                ret.setServlet(servlet);
                logger.info("put " + ret + " into " + name);
                actionMap.put(name, ret);
            }
        }

        return ret;
    }

    public Map getHomePageModules() {
        logger.debug("Get home Page Module List");
        if (homepageModuleMap != null) return homepageModuleMap;
        
        homepageModuleMap = new HashMap();
        for (Iterator it = moduleList.iterator(); it.hasNext(); ) {
            ModuleConfig bm = (ModuleConfig)it.next();
            List hl = bm.getHomepageModuleList();
            if (hl != null && hl.size() > 0) {
            	for (Iterator it2 = hl.iterator(); it2.hasNext(); ) {
            		HomepageModule hm = (HomepageModule)it2.next();
                    homepageModuleMap.put(hm.getName(), hm.getUrl());
                }
            }
        }
        return homepageModuleMap;
    }

    public Map getAdminModules() {
        logger.debug("Get Admin Page Module List");
        if (adminModuleMap != null) return adminModuleMap;
        
        adminModuleMap = new HashMap();
        for (Iterator it = moduleList.iterator(); it.hasNext(); ) {
            ModuleConfig bm = (ModuleConfig)it.next();
            List al = bm.getAdminModuleList();
            if (al != null && al.size() > 0) {
            	for (Iterator it2 = al.iterator(); it2.hasNext(); ) {
            		AdminModule am = (AdminModule)it2.next();
            		adminModuleMap.put(am.getName(), am.getBoxAdminModule());
                }
            }
        }
        return adminModuleMap;
    }

    public Iterator getAllModules() {
        return moduleList.iterator();
    }
    
    public ModuleConfig getModule(String name) {
        return (ModuleConfig)moduleMap.get(name);
    }

    public String toString() {
        String newline = System.getProperty("line.separator");
        StringBuffer buf = new StringBuffer();

        buf.append("Main Config").append(newline);

        buf.append( "--- Prop ---" ).append(newline);
        for (Iterator it = propMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry en = (Map.Entry)it.next();
            buf.append("key=").append((String)en.getKey());
            buf.append(" value=").append((String)en.getValue()).append(newline);
        }

        buf.append( "--- Module ---" ).append(newline);
        for (Iterator it = moduleMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry en = (Map.Entry)it.next();
            buf.append(en.getValue()).append(newline);
        }

        return buf.toString();
    }

    public static void debug(Object obj) {
        System.out.println("From MainConfig.java: " + obj);
    }
}
